<?php

namespace App\Service\UserCenter\v1_3_5;

use App\Helpers\DateUtil;
use App\Model\Stat\StatDailyHostInfo;
use App\Model\UserCoin;
use App\Service\BaseService;

/**
 * 主播数据详情
 */
class HostPerformDetailService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $date = $this->request->input('check_dt', '');
        $date = $date ?: date('Y-m-d');
        $data = StatDailyHostInfo::getData($userId, $date);
        foreach (StatDailyHostInfo::DETAIL_TYPE as $type => $item) {
            if (isset($data[$type])) {
                $value = $this->buildData($data[$type], $type);
            } else {
                $mondayTs = (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600);
                $monday = date('Y-m-d', $mondayTs);
                $value = '0';
                switch ($type) {
                    case 'week_income':
                        $value = StatDailyHostInfo::getIncomeData($userId, $monday);
                        break;
                    case 'total_income':
                        $userCoin = UserCoin::getUserInfo($userId);
                        $value = $userCoin ? $userCoin['total_income_coin'] : 0;
                        break;
                }
            }
            $ret['list'][] = [
                'type' => $item[0],
                'unit' => $item[1],
                'value' => (string)$value,
            ];
        }
        $ret['tip'] = '数据半小时更新一次';
        return $ret;
    }

    function buildData($data, $type = '')
    {
        switch ($type) {
            case 'online_time':
                return DateUtil::changeTimeType($data);
                //分钟
            case 'call_minutes':
                return DateUtil::changeTimeType($data * 60);
                //平均时间
            case 'average_call_time':
                return round($data / 60, 1);
        }
        return $data;
    }
}
